package com.wzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzx.common.pojo.resp.Result;
import com.wzx.common.util.date.DateUtil;
import com.wzx.user.mapper.RelationMapper;
import com.wzx.user.mapper.SystemMsgMapper;
import com.wzx.user.mapper.UserMapper;
import com.wzx.user.mongo.SendMsg;
import com.wzx.user.mongo.req.SysMsgReq;
import com.wzx.user.pojo.Relation;
import com.wzx.user.pojo.SystemMsg;
import com.wzx.user.pojo.User;
import com.wzx.user.service.SystemMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wzx
 * @version 1.0
 * @date 2023/6/2 - 10:40
 * @description 标题
 */
@Service
public class SystemMsgServiceImpl extends ServiceImpl<SystemMsgMapper, SystemMsg> implements SystemMsgService {

    @Autowired
    private SystemMsgMapper systemMsgMapper;

    @Autowired
    private RelationMapper relationMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result getMsgBySystem(SysMsgReq sysMsgReq) {
        Page<SystemMsg> page = new Page<>(sysMsgReq.getPage(), sysMsgReq.getSize());
        QueryWrapper<SystemMsg> queryWrapper = new QueryWrapper<>();
        if (1 == sysMsgReq.getMsgType()) {
            //我发送的消息
            queryWrapper.eq("senderId", sysMsgReq.getUserId());
        } else {
            //我接收的
            queryWrapper.eq("receiverId", sysMsgReq.getUserId());
        }
        queryWrapper.select().orderByDesc("createTime");

        Page<SystemMsg> systemMsgPage = systemMsgMapper.selectPage(page, queryWrapper);

        return Result.successByPage(systemMsgPage.getPages(),
                systemMsgPage.getSize(),
                systemMsgPage.getTotal(),
                systemMsgPage.getCurrent(),
                systemMsgPage.getRecords());
    }

    @Override
    public void cleanAll(Integer userId) {
        LambdaQueryWrapper<SystemMsg> lambdaQueryWrapper = new QueryWrapper<SystemMsg>().lambda();
        lambdaQueryWrapper.eq(SystemMsg::getSenderId, userId)
                .or()
                .eq(SystemMsg::getReceiverId, userId);

        systemMsgMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public void addFriend(SystemMsg systemMsg) {
        if (systemMsg.getState().equals("1")) {
            //同意添加好友
            Relation relation = new Relation();
            relation.setUid(systemMsg.getReceiverId());
            relation.setFid(systemMsg.getSenderId());
            //设置朋友备注
            //relation.setFriendRemarks(systemMsg.getSendName());
            relation.setType("2");
            relationMapper.insert(relation);
            relation.setUid(systemMsg.getSenderId());
            relation.setFid(systemMsg.getReceiverId());
            //为朋友设置备注
            //relation.setFriendRemarks("");
            relationMapper.insert(relation);

        }
        SystemMsg updateSystemMsg = systemMsgMapper.selectById(systemMsg.getId());
        updateSystemMsg.setState(systemMsg.getState());

        systemMsgMapper.updateById(updateSystemMsg);
    }

    @Override
    public void addGroup(SystemMsg systemMsg) {
        if (systemMsg.getState().equals("1")) {
            //同意添加进入群聊
            Relation relation = new Relation();
            relation.setUid(systemMsg.getReceiverId());
            relation.setFid(systemMsg.getGroupId());
            relation.setIdentity("3");
            relation.setType("3");
            relationMapper.insert(relation);
        }
        SystemMsg updateSystemMsg = systemMsgMapper.selectById(systemMsg.getId());
        updateSystemMsg.setState(systemMsg.getState());

        systemMsgMapper.updateById(updateSystemMsg);

    }
}
